package com.sinosoft.ptas.control;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sinosoft.ptas.control.dto.ReportDataToExcel;
import com.sinosoft.ptas.control.dto.RetrieveReportData;
import com.sinosoft.ptas.service.ReportService;
import com.sinosoft.ptas.system.util.ExportExcel;

/**
 * 申报表导出到Excel表格控制层
 * @author lianWuJun
 * @time   2017年3月17日 上午10:37:58
 */
@Controller
@RequestMapping(value="/outtoexcel")
public class OutToExcelController {
	
	@Autowired
	ReportService reportService;
	
	/**
	 * 查询申报导出给Excal
	 * @param reportDto
	 * @return
	 */
	@RequestMapping(value="/exportExcel")
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void exportExcel(RetrieveReportData reportDto,HttpServletResponse res) {
		List<ReportDataToExcel> list = reportService.retrieveReportForDOPAFToExcel(reportDto);
		ExportExcel ex = new ExportExcel();
        res.setContentType("application/vnd.ms-excel");
        String file = null;
		try {                                    //编码一定要用ISO8859-1,否则导出的格式不对
			file = new String(("区域目标绩效申报情况").getBytes(), "ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        res.setHeader("Content-disposition", "attachment;filename=" + file + ".xls");  
        res.addHeader("Pargam", "no-cache");  
        res.addHeader("Cache-Control", "no-cache");
        OutputStream ouputStream;
        HSSFWorkbook workBookExcel; 
		try {
			ouputStream = res.getOutputStream();
			workBookExcel = ex.exportExcel(list); 
			workBookExcel.write(ouputStream);
			ouputStream.flush();   
	        ouputStream.close(); 
		} catch (Exception e) {
			e.printStackTrace();
		}    
	}
}
